﻿using System;
using System.Collections.Generic;
using System.Text;
using YmSoft.Kblog.Model;
using YmSoft.Kblog.Utility;
using System.Data;
using System.Data.SqlClient;
using YmSoft.Kblog.IDAL;
namespace YmSoft.Kblog.DAL
{
    public class LoginService : ILoginService
    {
        public LoginService()
        {

        }
        /// <summary>
        /// 登录系统根据用户名和密码
        /// </summary>
        /// <param name="login"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public K_Login GetLoginInfoByLoginPass(string login, string password)
        {
            K_Login k_Login = null;
            DataTable Dt = new DataTable();
            SqlParameter[] parms = 
            { 
                new SqlParameter("@login",SqlDbType.NVarChar,16),
                new SqlParameter("@password",SqlDbType.NVarChar,50)
            };
            parms[0].Value = login.Trim();
            parms[1].Value = password.Trim();
            Dt = SQLHelper.ExecuteNonDataTable("select * from K_Login Where LoginName=@login and LoginPass=@password", CommandType.Text, parms);
            if (Dt.Rows.Count != 0)
            {
                k_Login = new K_Login();
                k_Login.LoginName = Dt.Rows[0]["LoginName"].ToString();
                k_Login.LoginPass = "登录密码已加密";
                k_Login.Role = int.Parse(Dt.Rows[0]["Role"].ToString());
                if (Dt.Rows[0]["LoginDate"].ToString() != null && Dt.Rows[0]["LoginDate"].ToString() != "")
                {
                    k_Login.LoginDate = DateTime.Parse(Dt.Rows[0]["LoginDate"].ToString());
                }
                k_Login.CreateDate = DateTime.Parse(Dt.Rows[0]["CreateDate"].ToString());
                SqlParameter[] dateparms = { new SqlParameter("@logindate", SqlDbType.DateTime) };
                dateparms[0].Value = DateTime.Now.ToString();
                SQLHelper.ExecuteNonQuery("update K_Login Set LoginDate=@logindate", CommandType.Text, dateparms);

            }
            return k_Login;
        }

        #region ILoginService 成员

        /// <summary>
        /// 获取菜单JSON
        /// </summary>
        /// <param name="tokey"></param>
        /// <returns></returns>
        public string GetMenuJSON(string tokey)
        {
            EasyUITools tool = new EasyUITools();
            DataSet ds = new DataSet();
            ds = SQLHelper.ExecuteNonDataSet("select * from K_Menu");
            return tool.Dataset2Json(ds);
        }

        #endregion
    }
}
